<template>
<view class="view_container">
	<view class="content">
		<u-form :model="form" ref="uForm" label-position="top">
			<u-form-item
				label="责任单位" 
				prop="company"
				:border-bottom="false">
				<u-input v-model="form.company" :border="true" />
			</u-form-item>
			<u-form-item
				label="数据报送质量得分" 
				prop="dataquality"
				:border-bottom="false">
				<u-input v-model="form.dataquality" :border="true" type="number"/>
			</u-form-item>
			<u-form-item
				label="截至本月计划投资完成占比(%)" 
				prop="fullInvestRate"
				:border-bottom="false">
				<u-input v-model="form.fullInvestRate" :border="true" type="number"/>
			</u-form-item>
			<u-form-item
				label="项目个数" 
				prop="proNum"
				:border-bottom="false">
				<u-input v-model="form.proNum" :border="true" type="number"/>
			</u-form-item>
			<u-form-item
				label="备注" 
				prop="remark" 
				:border-bottom="false">
				<u-input v-model="form.remark" :border="true" type="textarea" maxlength='1000'/>
			</u-form-item>
			<u-form-item
				label="月进度达标率" 
				prop="schedFullRate" 
				:border-bottom="false">
				<u-input 
					v-model="form.schedFullRate" 
					:border="true" 
					type="number" 
					maxlength='1000'/>
			</u-form-item>
			<u-form-item
				label="当前年月" 
				prop="thisMonth" 
				:border-bottom="false">
				<SelectDateBt
					v-model="form.thisMonth"
					:params="{
						year: true,
						month: true,
						day: false,
						hour: false,
						minute: false,
						second: false,
					}">
				</SelectDateBt>
			</u-form-item>
			<u-form-item
				label="年度计划投资(万元)" 
				prop="yearPlanInvest" 
				:border-bottom="false">
				<u-input v-model="form.yearPlanInvest" :border="true" type="number" maxlength='1000'/>
			</u-form-item>
			<u-form-item
				label="截至当前完成投资(万元)" 
				prop="yearfullInvest" 
				:border-bottom="false">
				<u-input v-model="form.yearfullInvest" :border="true" type="number" maxlength='1000'/>
			</u-form-item>
			<u-form-item
				label="总投资(万元)" 
				prop="allInvest"
				:border-bottom="false">
				<u-input v-model="form.allInvest" :border="true" type="number"/>
			</u-form-item>
			<u-form-item label=" " :border-bottom="false" v-if="!id">
				<u-button @click="onSave" type="primary">确定</u-button>
			</u-form-item>
			<u-form-item label=" " :border-bottom="false" v-if="id">
				<u-button @click="onUpdate" type="primary">修改</u-button>
			</u-form-item>
			<u-form-item label=" " :border-bottom="false" v-if="id">
				<u-button @click="onDelete" type="error">删除</u-button>
			</u-form-item>
		</u-form>
	</view>
	<u-toast ref="uToast" />
</view>
</template>

<script>
//新建月度汇总页面
import SelectDateBt from '@/components/public/SelectDateBt.vue';
import SelectTypeCp from "@/components/summery/SelectType.vue";
import {deleteSummary,getSummary,saveSummary} from "@/common/Api";
export default {
	components: {
		SelectDateBt,
		SelectTypeCp,
	},
	data() {
		return {
			id:'',
			form: {
				"allInvest":'',
				"company": "",
				"dataquality":'',
				"fullInvestRate":'',
				"proNum":'',
				"remark": "",
				"schedFullRate":'',
				"thisMonth": "",
				"yearPlanInvest":'',
				"yearfullInvest":'',
			},
			rules: {
				name: [{
					required: true,
					message: '不能为空',
					trigger: ['change', 'blur']
				}, ],
				type: [{
					required: true,
					message: '不能为空',
					trigger: ['change', 'blur']
				}, ],
			},
		}
	},
	onLoad(options){
		let id = options?options.id:undefined;
		this.id = id;
		if(id){
			this.getDataInfo(id);
		}
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	methods: {
		getDataInfo(id){  //根据id获取数据详情
			getSummary(id).then(res=>{
				let data = res.data.data;
				for(let index in this.form){
					this.form[index] = data[index];
				}
			});
		},
		onSave(){  //保存
			this.$refs.uForm.validate(valid => {
				if (!valid) return;
				saveSummary(
					this.form,
				).then(res=>{
					uni.$emit('addSummery');
					uni.navigateBack();
				});
			});
		},
		onUpdate(){  //修改
			saveSummary({
				...this.form,
				id:this.id,
			}).then(res=>{
				getSummary(this.id).then(res=>{
					let data = res.data.data;
					uni.$emit('updateSummery',data);
					uni.navigateBack();
				});
			});
		},
		onDelete(){  //删除
			deleteSummary(
				this.id,
			).then(()=>{
				uni.$emit('deleteSummery',this.id);
				uni.navigateBack();
			});
		},
	}
}
</script>

<style lang="scss" scoped>
.view_container {
	display: flex;
	flex-direction: column;
	justify-content: space-between;
	align-items: flex-start;
	.content {
		background-color: #fff;
		width: 100vw;
		padding: 0 15px;
		padding-bottom: 30px;
	}
}
</style>
